<?php defined('SYSPATH') OR die('No direct access allowed.');

class Useradmin_Controller extends Controller {

    function index(){
        $db = new Database;
        
        if($_POST){
            if(isset($_POST['Add'])){
                if($_POST['newname']){
                    $sql = "insert into users (name) values(?)";
                    $query = $db->query($sql, array($_POST['newname']));
                }
            }
            else{
                foreach ($_POST as $key => $value){
                    if(preg_match('/Delete(?<id>\d*)/',$key, $matches)){
                        $sql = "delete from users where id = ?";
                        $query = $db->query($sql, array($matches['id']));
                    }
                }
            }
        }
        
        $view = new View('useradmin');
        $sql = "select * from users";
        $view->set('users', $db->query($sql));
        
        $view->render(TRUE);
    }
}
?>